package com.ymatou.shop.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.ymatou.shop.Constants;
import com.ymatou.shop.model.LoginResult;
import com.ymatou.shop.model.Order;
import com.ymatou.shop.model.Product;
import com.ymatou.shop.model.Seller;
import com.ymatou.shop.ui.activity.SendCommentActivity;
import com.ymatou.shop.ui.activity.UserinfoActivity;
import com.ymatou.shop.ui.activity.buyer.PayOrderActivity;
import com.ymatou.shop.ui.activity.buyer.SaveOrderActivity;
import com.ymatou.shop.ui.activity.seller.EventDetailActivity;
import com.ymatou.shop.ui.activity.seller.EventListActivity;

/**
 * Some common global utilities.
 */
public final class GlobalUtil {
	
	// This class should not be instantiated, hence the private constructor
	private GlobalUtil() {}
	
	public static void logE(String message, Throwable tr) {
		Log.e(Constants.TAG, message, tr);
	}
	
	public static void logE(String message) {
		Log.e(Constants.TAG, message);
	}
	
	public static void logE(Context context, String message) {
		logE(message);
		//MobclickAgent.reportError(context, message) ;
	}
	
	public static void logE(Context context, String message, Throwable tr) {
		logE(message, tr);
		//MobclickAgent.reportError(context, message + "\n" + Log.getStackTraceString(tr)) ;
	}
	
	public static void logD(String message) {
		if (Constants.LOGD) {
			Log.d(Constants.TAG, message);
		}
	}
	
	public static void logW(String message) {
		if (Constants.LOGW) {
			Log.w(Constants.TAG, message);
		}
	}
	
	public static void startActivity(Context context, Class<?> class1) {
        Intent intent = new Intent();
        intent.setClass(context, class1);
        context.startActivity(intent);
    }
	
	public static void startActivity(Context context, Intent intent) {
		context.startActivity(intent);
	}
	
	public static void startActivityForResult(Activity context, Intent intent, int requestCode) {
		context.startActivityForResult(intent, requestCode);
	}
	
	public static void startActivity(Activity context, Class<?> class1, int enterAnim, int exitAnim) {
        Intent intent = new Intent();
        intent.setClass(context, class1);
        context.startActivity(intent);
        context.overridePendingTransition(enterAnim, exitAnim);
    }
	
	public static void startActivityForResult(Activity context,
			Class<?> class1, int requestCode) {
		Intent intent = new Intent();
		intent.setClass(context, class1);
		context.startActivityForResult(intent, requestCode);
	}
	
	/**
	 * Get version code for this application.
	 * 
	 * @param context	The context to use.  Usually your {@link android.app.Application}
     *               	or {@link android.app.Activity} object.
	 * @return the version code or -1 if package not found
	 */
	public static int getVersionCode(Context context) {
		int versionCode;
		try {
			versionCode = context.getPackageManager()
							.getPackageInfo(context.getPackageName(), 0).versionCode;
		} catch (Exception e) {
			versionCode = -1;
		}

		return versionCode;
	}
	
	/**
	 * Get version name for this application.
	 * 
	 * @param context	The context to use.  Usually your {@link android.app.Application}
     *               	or {@link android.app.Activity} object.
	 * @return the version name or empty string if package not found
	 */
	public static String getVersionName(Context context) {
		String versionName = null;
		try {
			versionName = context.getPackageManager()
							.getPackageInfo(context.getPackageName(), 0).versionName;
		} catch (Exception e) {
			versionName = StringUtil.EMPTY_STRING;
		}

		return versionName;
	}
	
	/**
     * hide keyboard
     */
	public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm == null)
			return;
        
		View v = activity.getCurrentFocus();
		if (v == null)
			return;

//        IBinder token = mSearchEdit.getWindowToken();
//        imm.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

	public static boolean isExternalMediaMounted() {
		if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            // No SD card found.
            Log.d(Constants.TAG, "no external storage");
            return false;
        }
        return true;
	}
	
	public static void safeShowDialog(Activity activity, int id) {
		try {
			activity.showDialog(id);
		} catch (Throwable e) {
		}
	}

	public static void safeDismissDialog(Activity activity, int id) {
		try {
			activity.dismissDialog(id);
		} catch (Throwable e) {
		}
	}
	
	public static void shortToast(Context context, int resId) {
		toast(context, context.getString(resId), Toast.LENGTH_SHORT);
	}
	
	public static void shortToast(Context context, CharSequence text) {
		toast(context, text, Toast.LENGTH_SHORT);
	}
	
	public static void longToast(Context context, int resId) {
		toast(context, context.getString(resId), Toast.LENGTH_LONG);
	}

	public static void longToast(Context context, CharSequence text) {
		toast(context, text, Toast.LENGTH_LONG);
	}
	
	/**
     * Make a toast.
     *
     * @param context  The context to use.  Usually your {@link android.app.Application}
     *                 or {@link android.app.Activity} object.
     * @param text     The text to show.  Can be formatted text.
     * @param duration How long to display the message.  Either {@link #LENGTH_SHORT} or
     *                 {@link #LENGTH_LONG}
     *
     */
	public static void toast(Context context, CharSequence text, int duration) {
//		View v = context.getLayoutInflater().inflate(R.layout.toast, null);
//		((TextView) v.findViewById(android.R.id.message)).setText(text);
		
		Toast.makeText(context, text, duration).show();
		
//		TextView v = new TextView(context);
//		v.setText(text);
//		
//		Toast toast = new Toast(context);
//		toast.setGravity(Gravity.CENTER, 0, 50);
//		toast.setDuration(duration);
//		toast.setView(v);
//		toast.show();
	}

	public static void startEventListActivity(Context context, int sellerId) {
		Intent intent = new Intent(context, EventListActivity.class);
		intent.putExtra(EventListActivity.EXTRA_SELLER_ID, sellerId);
		context.startActivity(intent);
	}
	
	public static void startSaveOrderActivity(Context context, Product product) {
		
		Intent intent = new Intent(context, SaveOrderActivity.class);
		intent.putExtra(SaveOrderActivity.PRODUCT_KEY, product);
		context.startActivity(intent);
	}
	
	public static void startPayOrderActivity(Context context, Product product, LoginResult loginResult, float redMoney) {
		
		Intent intent = new Intent(context, PayOrderActivity.class);
		intent.putExtra(PayOrderActivity.PRODUCT_KEY, product);
		intent.putExtra(PayOrderActivity.LOGIN_RESULT_KEY, loginResult);
		intent.putExtra(PayOrderActivity.RED_MONEY_KEY, redMoney);
		context.startActivity(intent);
	}
	
	public static void startSendCommentActivity(Context context, String productId) {
		
		Intent intent = new Intent(context, SendCommentActivity.class);
		intent.putExtra(SendCommentActivity.PRODUCTID_KEY, productId);
		context.startActivity(intent);
	}
	
	public static void startEventDetailActivity(Context context, String productId, Seller seller) {
		Intent intent = new Intent(context, EventDetailActivity.class);
		intent.putExtra(Constants.EXTRA_ID, productId);
		intent.putExtra(Constants.EXTRA_DATA, seller);
		context.startActivity(intent);
	}
	
	public static void startUserInfoActivity(Context context) {
		Intent intent = new Intent(context, UserinfoActivity.class);
		context.startActivity(intent);
	}
}
